package com.allianz.webapps.controllers;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONNull;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.support.json.JSONParser;
import com.allianz.frame.utils.ApplicationContextUtil;
import com.allianz.webapps.https.interfaces.type001.Type001Login;
import com.allianz.webapps.https.interfaces.type001.TypeSearch;
import com.allianz.webapps.https.interfaces.type001.pojo.User;
import com.allianz.webapps.https.interfaces.util.HttpsUtil;
import com.allianz.webapps.orderspojo.AppBase;
import com.allianz.webapps.orderspojo.ManagerSearch;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
@Controller
@RequestMapping(value="ordermanager")
public class OrderController {
	
	//http://localhost:8080/pad_marketing/ordermanager/index.action
	@RequestMapping(value="index")
	public String order(HttpServletRequest request,HttpServletResponse response){
		return "views/page/orderMange";
	}
	@RequestMapping(value="list")
	public String list(HttpServletRequest request,HttpServletResponse response){
		return "views/page/orderMangeList";
	}
	@RequestMapping(value="name")
	public String name(HttpServletRequest request,HttpServletResponse response){
		return "views/page/orderMangeName";
	}
	@RequestMapping(value="date")
	public String date(HttpServletRequest request,HttpServletResponse response){
		return "views/page/orderMangeDate";
	}	
	@RequestMapping(value="search")
	@ResponseBody
	public String search(HttpServletRequest request,HttpServletResponse response) throws Exception{
		HashMap<String,Object> map = new HashMap<String,Object>();
		String managerSearch = request.getParameter("jsonParam");		
		JSONObject searchJson = JSONObject.fromObject(managerSearch);
		User u = (User) ApplicationContextUtil.getSession("username");	
		ManagerSearch m = (ManagerSearch) JSONObject.toBean(searchJson, ManagerSearch.class);
		String requestLoginParam = TypeSearch.requestLoginParam(u,m);	
		String body = HttpsUtil.httpsPost(requestLoginParam);
		return body;
	}
	
	@RequestMapping(value="searchAll")
	@ResponseBody
	public String searchAll(HttpServletRequest request,HttpServletResponse response) throws Exception{
		HashMap<String,Object> map = new HashMap<String,Object>();		
		User u = (User) ApplicationContextUtil.getSession("username");	
		String requestLoginParam = TypeSearch.requestLoginParam(u);	
		String body = HttpsUtil.httpsPost(requestLoginParam);
		return body;
	}
	@RequestMapping(value="delete")
	@ResponseBody
	public String delete(HttpServletRequest request,HttpServletResponse response) throws Exception {
		HashMap<String,Object> map = new HashMap<String,Object>();
		String jsonParam = request.getParameter("jsonParam");
		User u = (User) ApplicationContextUtil.getSession("username");	
		JSONObject js = JSONObject.fromObject(jsonParam);
		AppBase appBase = (AppBase)JSONObject.toBean(js,AppBase.class);
		appBase.setUser(u);
		String requestLoginParam;		
		requestLoginParam = TypeSearch.retLoginParam(appBase,"app.delete");
		String body = HttpsUtil.httpsPost(requestLoginParam);		
		return body;
	}
	@RequestMapping(value="copy")
	@ResponseBody
	public String copy(HttpServletRequest request,HttpServletResponse response) throws Exception{
		HashMap<String,Object> map = new HashMap<String,Object>();
		String jsonParam = request.getParameter("jsonParam");
		User u = (User) ApplicationContextUtil.getSession("username");	
		JSONObject js = JSONObject.fromObject(jsonParam);
		AppBase appBase = (AppBase)JSONObject.toBean(js,AppBase.class);
		appBase.setUser(u);
		String requestLoginParam  = TypeSearch.retLoginParam(appBase,"app.copy");
		String body = HttpsUtil.httpsPost(requestLoginParam);			
		return body;
	}	
}